package com.xsx.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

public class parameterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        String name = req.getParameter("username");
        System.out.println("用户名:" + name);

        String pwd = req.getParameter("pwd");
        System.out.println("密码：" + pwd);

        //获取多个请求参数
        String[] hobby = req.getParameterValues("hobby");
        System.out.println("兴趣:"+ Arrays.asList(hobby));
//        for (int i = 0; i < hobby.length; i++) {
//            System.out.println("兴趣：" + hobby[i]);
//        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //设置请求体字符集为UTF-8 解决post请求的中文乱码问题 要在参数调用之前才有效
        req.setCharacterEncoding("UTF-8");

        System.out.println("----------------------doPost-------------------");
        //获取请求参数
        String name = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        //获取多个请求参数
        String[] hobby = req.getParameterValues("hobby");

        System.out.println("用户名:" + name);
        System.out.println("密码：" + pwd);
        System.out.println("兴趣:"+ Arrays.asList(hobby));


    }
}
